﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    /// <summary>
    /// 教师 事务逻辑
    /// 
    /// 注：教师信息直接在数据库中添加。或教师可以添加其他教师
    /// </summary>
    public class TeatherBS
    {
        /// <summary>
        /// 教师是否存在
        /// </summary>
        static public bool IsExists(string teacher_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var teacher = db.teacher.FirstOrDefault(x => x.teacher_id == teacher_id);
                if (teacher == null)
                    return false;
                else
                    return true;
            }
        }

        /// <summary>
        /// 获取教师个人信息
        /// </summary>
        /// <param name="teacher_id">教师ID</param>
        /// <returns>Model.Teacher</returns>
        static public Teacher GetTeacherInfo(string teacher_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Teacher model = new Teacher();
                //var ret = db.teacher.Where(x => x.teacher_id == teacher_id);
                var teacher = db.teacher.FirstOrDefault(x => x.teacher_id == teacher_id);
                model.teacher_name = teacher.teacher_name;
                model.teacher_title = teacher.teacher_title;
                model.wage_id = teacher.wage_id;
                return model;
            }
        }

        /// <summary>
        /// 获取老师带课列表
        /// </summary>
        /// <param name="teacher_id"></param>
        /// <returns></returns>
        static public List<Course> GetCourseList(string teacher_id)
        {
            return CourseBS.GetCourseList(teacher_id);
        }

        /// <summary>
        /// 新增教师
        /// </summary>
        /// <returns>成功返回true</returns>
        static public bool AddTeacher(Teacher model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.teacher.AddObject(new teacher()
                    {
                        teacher_id = model.wage_id,
                        teacher_name = model.teacher_name,
                        teacher_title = model.teacher_title,
                        wage_id = model.wage_id,
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 老师新增带课
        /// </summary>
        /// <param name="teacher_id">老师ID</param>
        /// <param name="course_id">课程ID</param>
        /// <returns>是否成功</returns>
        static public bool AddCourse(string teacher_id, string course_id)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    //var ret = db.teacher.Where(x => x.teacher_id == teacher_id);
                    var teacher = db.teacher.FirstOrDefault(x => x.teacher_id == teacher_id);
                    if (teacher.course == null) //外键表，要自己new不？
                    {
                        teacher.course = new System.Data.Objects.DataClasses.EntityCollection<course>();
                    }
                    var target = db.course.FirstOrDefault(c => c.course_id == course_id);
                    teacher.course.Add(target);
                    db.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
